<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="keyword" placeholder="关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getQueryShops">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="exportExcel">导出</el-button>
        </el-form-item>
        <el-form-item>
          <!-- 默认name="file" -->
          <el-upload class="upload-demo"
                     action="http://localhost:8080/shop/import"
                     list-type="text">
            <el-button type="warning">导入</el-button>
          </el-upload>
        </el-form-item>

      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="shops" highlight-current-row v-loading="listLoading" @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
      <el-table-column prop="name" label="店铺名称" width="90" align="center"></el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="130" align="center"></el-table-column>
      <el-table-column prop="registerTime" label="入驻时间" width="140" align="center"></el-table-column>
      <el-table-column prop="address" label="店铺地址" width="230" align="center"></el-table-column>
      <el-table-column prop="employee.username" label="店铺管理员" width="90" align="center"></el-table-column>

      <el-table-column prop="state" label="店铺状态" width="130" align="center">
        <template slot-scope="scope">
          <span style="color: orange" v-if="scope.row.state==1">待审核</span>
          <span style="color: blue" v-else-if="scope.row.state==2">审核通过待激活</span>
          <span style="color: green" v-else-if="scope.row.state==3">激活成功</span>
          <span style="color: red" v-else-if="scope.row.state==4">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="logo" label="图标" width="80" align="center">
        <template scope="scope">
          <img :src="imgPrefix+scope.row.logo" width="50px">
        </template>
      </el-table-column>

      <el-table-column label="操作" width="240" align="center">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button size="small" type="warning" :disabled="scope.row.state!==1" @click="handleAudit(scope.$index, scope.row)">店铺审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize"
                     :total="totals" :current-page="currentPage" style="float:right;">
      </el-pagination>
    </el-col>

    <!--新增或编辑对话框-->
    <el-dialog :title="title" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="100px" :rules="saveFormRules" ref="saveForm">
        <el-form-item label="店铺名称" prop="username">
          <el-input v-model="saveForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺电话" prop="phone">
          <el-input v-model="saveForm.tel" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺地址" prop="">
          <el-input v-model="saveForm.address" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺管理员">
          <el-select v-model="saveForm.admin_id" placeholder="请选择">
            <el-option v-for="item in employees" :label="item.username" :value="item.id">
              <span style="float: left">{{ item.username }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.state">
            <el-radio class="radio" :label="1">待审核</el-radio>
            <el-radio class="radio" :label="2">审核通过待激活</el-radio>
            <el-radio class="radio" :label="3">激活成功</el-radio>
            <el-radio class="radio" :label="4">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="saveSubmit" :loading="saveLoading">提交</el-button>
      </div>
    </el-dialog>

    <!-- 审核模态框 -->
    <el-dialog title="审核店铺" :visible.sync="shopAuditVisible" :close-on-click-modal="false">
      <el-form :model="shopAuditLog" label-width="80px"  ref="shopAuditLogForm">
        <el-form-item label="备注" prop="note">
          <el-input type="textarea" v-model="shopAuditLog.note"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="shopAuditVisible = false">取消</el-button>
        <el-button type="primary" @click.native="auditPass">通过</el-button>
        <el-button type="primary" @click.native="auditReject">驳回</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>

export default {
  data() {
    return {

      //审核模态框
      shopAuditVisible: false,
      //编辑界面数据
      shopAuditLog: {
        shopId: null,
        note:''
      },

      //图片显示的前缀
      imgPrefix: "http://123.207.27.208",

      //高级查询条件
      keyword: '',

      //店铺数据
      shops: [],

      //员工数据
      employees: [],

      //分页查询条件
      totals: 0,
      currentPage: 1,
      pageSize: 5,

      //忙等框，默认关闭
      listLoading: false,

      //列表选中列，数组
      sels: [],


      //添加或编辑界面是否显示
      saveFormVisible: false,
      saveLoading: false,

      //添加或编辑界面标题
      title: '',

      //验证店铺编号和店铺名称
      saveFormRules: {
        name: [
          {required: true, message: '请输入店铺名称', trigger: 'blur'}
        ],
        tel: [
          {required: true, message: '请输入店铺电话', trigger: 'blur'}
        ]
      },

      //添加或编辑界面数据
      saveForm: {
        id: null,
        name: '',
        tel: '',
        registerTime: '',
        state: 1,
        address: '',
        logo: '',
        admin_id: ''

      }
    }
  },
  methods: {

    exportExcel(){
      location.href="http://localhost:8080/shop/export";
    },

    //点击店铺审核弹出模态框
    handleAudit: function (index, row) {

      this.shopAuditLog.shopId = row.id;
      this.shopAuditVisible = true;

    },
//审核驳回
    auditReject(){
      this.$http.post("/shop/audit/reject",this.shopAuditLog).then(result=>{
        if(result.data.success){
          this.$message({
            message: '驳回成功',
            type: 'success'
          });
        }else{
          this.$message({
            message: '驳回失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      }).catch(result=>{
        this.$message.error("系统繁忙,请稍后再试。。。");
      })
    },
    //审核通过
    auditPass(){
      this.$http.post("/shop/audit/pass",this.shopAuditLog).then(result=>{
        result = result.data;
        if(result.success){
          this.$message({
            message: '审核通过',
            type: 'success'
          });
        }else{
          this.$message({
            message: '审核失败',
            type: 'error'
          });
        }
        this.shopAuditVisible = false;
        this.getShops();
      }).catch(result=>{
        this.$message.error("系统繁忙,请稍后再试。。。");
      })
    },


    //点击页数，重新赋值currentPage，刷新页面获取店铺列表
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.getShops();
    },

    //高级查询方法
    getQueryShops() {
      //高级查询成功之后跳转到第一页，避免BUG
      this.currentPage = 1;
      this.getShops();
    },

    //获取店铺列表
    getShops() {
      let para = {
        currentPage: this.currentPage,	//当前页
        pageSize: this.pageSize, //每页显示的条数
        keyword: this.keyword//高级查询条件
      };
      this.listLoading = true;			//显示加载框/忙等框
      this.$http.post("/shop", para).then(result => {
        result = result.data;
        //给totals赋值 - 计算分页栏
        this.totals = result.totals;
        //赋值分页数据 - 显示当前页的分页数据
        this.shops = result.data;
        //隐藏加载框/忙等框
        this.listLoading = false;
      }).catch(res => {
        this.$message.error("系统超时，请稍后重试!");

      });
    },
    //获取员工 - 所有的员工
    getemployees() {
      this.$http.get("/employee").then(res => {
        this.employees = res.data;
      })
    },


    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.delete("/shop/" + row.id).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("删除成功");

            //计算总页数. 如果 总数量%每页条数==0 ? 总数量/每页条数 : 总数量/每页条数(向上取整)
            let totalPage = this.totals % this.pageSize == 0 ? this.totals / this.pageSize
                : Math.ceil(this.totals / this.pageSize);

            //   不在第一页           当前页码==总页数==最后一页       删除一条数据后的总数量%每页显示数量==0==当前页最后一条数据
            if (this.currentPage > 1 && this.currentPage == totalPage && (this.totals - 1) % this.pageSize == 0) {
              //返回上一页
              this.currentPage = this.currentPage - 1;
            }
            //重新获取列表数据
            this.getShops();
          } else {
            this.$message.error("删除失败");
          }
        }).catch(res => {
          this.$message.error("系统繁忙,请稍后再试。。。");
        });
      }).catch(() => {

      });
    },

    //点击编辑，显示编辑界面
    handleEdit: function (index, row) {
      this.title = '编辑';
      //克隆数据回显
      this.saveForm = Object.assign({}, row);

      //获取所有员工
      this.getemployees();
      //获取所有店铺
      this.getShops();
      //显示模态框
      this.saveFormVisible = true;
    },

    //点击新增，显示新增界面
    handleAdd: function () {
      this.title = '新增';
      //清空数据
      this.saveForm = {
        id: null,
        name: '',
        tel: '',
        registerTime: '',
        state: 1,
        address: '',
        logo: '',
        admin_id: ''

      };
      //获取所有员工
      this.getemployees();
      //获取所有店铺
      this.getShops();
      //显示模态框
      this.saveFormVisible = true;
    },

    //添加和编辑
    saveSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        //判断数据验证是否通过
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //开启加载框
            this.saveLoading = true;
            //克隆数据
            let para = Object.assign({}, this.saveForm);
            //向接口发送put请求
            this.$http.put("/shop", para).then(res => {
              //关闭模态框
              this.saveFormVisible = false;
              //关闭加载框
              this.saveLoading = false;
              if (res.data.success) {
                this.$message.success("操作成功");
                //重新获取列表数据
                this.getShops();
              } else {
                this.$message.error("操作失败");
              }
            }).catch(res => {
              this.$message.error("系统繁忙,请稍后再试。。。");

            })


          });
        }
      });
    },

    //多选框改变事件，this.sels.length大于0才能点击批量删除按钮
    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        this.$http.patch("/shop", ids).then(res => {
          this.listLoading = false;
          if (res.data.success) {
            this.$message.success("批量删除成功");

            //计算总页数. 如果 总数量%每页条数==0 ? 总数量/每页条数 : 总数量/每页条数(向上取整)
            let totalPage = this.totals % this.pageSize == 0 ? this.totals / this.pageSize
                : Math.ceil(this.totals / this.pageSize);

            //   不在第一页       当前页码==总页数==最后一页       删除选中条数据后的总数量%每页显示数量==0==当前页全部删除完了
            if (this.currentPage > 1 && this.currentPage == totalPage && (this.totals - ids.length) % this.pageSize == 0) {
              //返回上一页
              this.currentPage = this.currentPage - 1;
            }
            //重新获取全部数据
            this.getShops();
          } else {
            this.$message.error("批量删除失败");
          }
        }).catch(res => {
          this.$message.error("系统繁忙,请稍后再试。。。");
        });
      }).catch(() => {

      });
    }
  },
  mounted() {
    //钩子函数 页面一加载就获取列表数据显示在页面
    this.getShops();
  }
}

</script>

<style>

</style>